package org.ws.task.event;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.Executors;

public class AsyncEventBusHolder<T extends EventSource> implements EventBusHolder<T> {

    private final AsyncEventBus asyncEventBus;

    public AsyncEventBusHolder(String id){
        ThreadFactoryBuilder factoryBuilder = new ThreadFactoryBuilder();
        factoryBuilder.setDaemon(false);
        factoryBuilder.setNameFormat("AsyncEventBusThread-"+id+"-%d");
        this.asyncEventBus = new AsyncEventBus(id, Executors.newFixedThreadPool(4,factoryBuilder.build()));
    }

    @Override
    public void registerListener(EventListener<T> listener){
        this.asyncEventBus.register(listener);
    }

    @Override
    public void publishEvent(T event){
        this.asyncEventBus.post(event);
    }
}
